package com.tankGame;

import java.awt.*;

/**
 * @author LingLed
 * @version 1.0
 * 玩家坦克
 */
public class Hero extends Tank{
    //玩家坦克
    public Hero(int x, int y, int type) {
        super(x, y,type);
    }
    public void draw(Graphics g){
        if(g == null){
            throw new RuntimeException("g不能为空");
        }
        super.drawTank(getX(),getY(),g,getDirect(),getType());
    }

    private boolean isFire = false;
    private int fireSpeed = 500;
    private Thread t1;
    @Override
    public void fire() {
        if(isFire){
            //isFire为true说明已经在射击中,无需重复执行
            return;
        }
        //控制射速
        t1 = new Thread(){
            @Override
            public void run() {
                do{
                    isFire = true;
                    Hero.super.fire();
                    try {
                        Thread.sleep(fireSpeed);
                    } catch (InterruptedException e) {
                        break;
                    }
                }while (isFire);
            }
        };
        t1.start();
    }
    public void stopFire(){
        isFire = false;
        t1.interrupt();
        t1 = null;
    }

    public void setFireSpeed(int fireSpeed) {
        this.fireSpeed = fireSpeed;
    }
}
